#include "PTScene.h"
#include "ImageManager.h"
#include "SimpleAudioEngine.h"
#include "ImageSprite.h"
#include "GameData.h"
#include "SceneManager.h"
#include "ThemeScene.h"
#include "MusicController.h"

using namespace cocos2d;
using namespace CocosDenshion;

enum{
	kTagNode,
	kTagShadowBg,
	kTagWin,
};

CCScene* PTScene::scene(const char * path) {
	CCScene *scene = CCScene::create();
	PTScene *layer = PTScene::create(path);
	scene->addChild(layer);
	return scene;
}

// on "init" you need to initialize your instance
bool PTScene::init() {
	//////////////////////////////
	// 1. super init first
	if (!CCLayer::init()) {
		return false;
	}

	//char* imagePath = "theme/girls/beauty_1.png";

	CCSize size = CCDirector::sharedDirector()->getWinSize();

	ImageManager::initRandomGrid(m_randomPos);

	setTouchEnabled(true);
	setKeypadEnabled(true);

	pBatchNode = CCSpriteBatchNode::create(m_imagePath, 9);

	CCSize contentSize = pBatchNode->getTexture()->getContentSizeInPixels();

	int itemWidth = (int) (contentSize.width / GRID_LENGTH);
	int itemHeight = (int) (contentSize.height / GRID_LENGTH);

	pBatchNode->setAnchorPoint(ccp(0, 0));
	pBatchNode->setPosition(
			ccp((size.width - contentSize.width) / 2,
					(size.height - contentSize.height) / 2));

	this->addChild(pBatchNode, 0);

	unsigned int w = 0;
	for (int i = 0; i < GRID_LENGTH; i++) {
		for (int j = 0; j < GRID_LENGTH; j++) {
			ImageSprite *_pImageSprite = ImageSprite::create(
					pBatchNode->getTexture(), i, j, itemWidth, itemHeight);
			if (_pImageSprite) {
				_pImageSprite->setFinalPos(w);
				_pImageSprite->setCurrentPos(m_randomPos[i][j]);
				_pImageSprite->setRandomPos(m_randomPos[i][j]);
				w++;

				_pImageSprite->setAnchorPoint(ccp(0, 0));
				int n = m_randomPos[i][j] / GRID_LENGTH;
				int m = m_randomPos[i][j] % GRID_LENGTH;
				_pImageSprite->setPosition(
						ccp(m * itemWidth, (GRID_LENGTH - 1 - n) * itemHeight));

				pBatchNode->addChild(_pImageSprite);

			}

		}
	}


	CCNode * winNode = CCNode::create();

	//shadow
	CCLayerColor * shadowBg = CCLayerColor::create(ccc4(0, 0,0,150));
	winNode->addChild(shadowBg,0,kTagShadowBg);
	//win.png
	CCSprite * winSprite = CCSprite::create("win.png");
	winSprite->setPosition(
			ccp(CCDirector::sharedDirector()->getWinSize().width / 2,
					CCDirector::sharedDirector()->getWinSize().height / 2));
	winNode->addChild(winSprite,1,kTagWin);
	//continue button
	CCMenuItemImage * continueButton = CCMenuItemImage::create("scene/btn-continue-normal.png",
			"scene/btn-continue-pressed.png", this,
			menu_selector(PTScene::onContinue));
	//select button
	CCMenuItemImage * selectButton = CCMenuItemImage::create("scene/btn-select-normal.png",
				"scene/btn-select-pressed.png", this,
				menu_selector(PTScene::onSelectTheme));
	//quit button
	CCMenuItemImage * quitButton = CCMenuItemImage::create("scene/btn-quit-normal.png",
					"scene/btn-quit-pressed.png", this,
					menu_selector(PTScene::onQuit));
	CCMenu * menu = CCMenu::create(continueButton,selectButton,quitButton,NULL);
	menu->alignItemsVerticallyWithPadding(10);
	menu->setPosition(
			ccp(CCDirector::sharedDirector()->getWinSize().width / 2,
					CCDirector::sharedDirector()->getWinSize().height / 2  - 40));
	winNode->addChild(menu,3);
	//select button

	winNode->setVisible(false);
	addChild(winNode,2,kTagNode);

	return true;
}

void PTScene::menuCloseCallback(CCObject* pSender) {
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}

void PTScene::onEnter() {
	//CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,1, false);
	CCLayer::onEnter();
}

void PTScene::registerWithTouchDispatcher() {
	//CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
			this, 1, false);
}

bool PTScene::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {
	pBeginTouch = pTouch;
	CCArray *ccArray = pBatchNode->getChildren();
	CCObject *obj;
	bool tag = false;
	CCARRAY_FOREACH(ccArray, obj)
	{
		ImageSprite *imageSprite = (ImageSprite*) obj;
		if (imageSprite->containsTouchLocation(pTouch)) {
			tag = true;
			pSelectImage = imageSprite;
			break;
		} else {
			//CCLog("touch no sprite");
		}
	}
	if (tag == false) {
		pSelectImage = NULL;
	}
	return true;
}

void PTScene::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) {
}

void PTScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
	CCLog("NULL--------------------------------------------------");
	CCArray * ccArray = pBatchNode->getChildren();
	CCObject *obj;
	CCARRAY_FOREACH(ccArray, obj)
	{
		ImageSprite *imageSprite = (ImageSprite *) obj;
		bool result = imageSprite->containsTouchLocation(pTouch);
		if (result && pSelectImage != NULL) {

			if (imageSprite->getPosition().equals(
					pSelectImage->getPosition())) {
				return;
			} else if (pSelectImage->nextContainsTouchLocation(pTouch)) {

				CCPoint point = imageSprite->getPosition();
				imageSprite->setPosition(pSelectImage->getPosition());
				pSelectImage->setPosition(point);

				int temp = imageSprite->getCurrentPos();
				imageSprite->setCurrentPos(pSelectImage->getCurrentPos());
				pSelectImage->setCurrentPos(temp);

				if (ImageManager::isWin(ccArray)) {
					CCNode * winNode =  getChildByTag(kTagNode);
					winNode->setVisible(true);
				} else {

				}
				return;
			} else {
			}
		}
	}
}

PTScene * PTScene::create(const char * path) {
	PTScene * scene = new PTScene();
	scene->m_imagePath = path;
	scene->init();
	scene->autorelease();
	return scene;
}

void PTScene::onContinue(CCObject * pSender) {
	const char * path = GameData::getInstance()->getNextThemeItem();
	CCScene * scene = SceneManager::getScene(path);
	CCDirector::sharedDirector()->replaceScene(scene);
}

void PTScene::onSelectTheme(CCObject * pSender){
	CCScene * scene = ThemeScene::scene(kTheme,-1);
	CCDirector::sharedDirector()->replaceScene(scene);
}

void PTScene::onQuit(CCObject * pSender){
	quit();
}

void PTScene::quit(){

	GameData::getInstance()->release();
	MusicController::getInstance()->release();
	CCTextureCache::sharedTextureCache()->removeAllTextures();

	CCDirector::sharedDirector()->end();
}

void PTScene::keyBackClicked() {
	quit();
}
